<template>
  <div class="contail">
    <van-nav-bar class="header"
                 title="我的小区"
                 left-arrow
                 @click-left="goBack()">
      <van-icon name="add-o"
                slot="right"
                @click="goPage('/auth','',{type:1})" />
    </van-nav-bar>
    <div class="fengniao-tip"
         v-if="tip">暂时没有数据</div>
    <div class="weaper">
      <div v-for="(item,index) in list"
           :key="index"
           class=" fengniao-list">
        <div class="fengniao-content item"
             @click="setDefaultHouse(item,index)">
          <div class="header">
            <span class="title">
              {{item.communityName}}&nbsp;&nbsp;
            </span>
            <el-tag type="danger"
                    size="small"
                    v-if="item.defaultHouse==1">默认</el-tag>
          </div>
          <div class="date">
            {{item.buildingName}}&nbsp;{{item.unitName}}&nbsp;{{item.houseName}}房间
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getList, setDefaultHouse } from '@/api/community/index';
import { validatenull } from '@/util/validate';
import baseCommon from '@/mixins/common';
import filterCommon from '@/mixins/filter';
import Bus from '@/util/bus.js';

export default {
  name: 'communityContail',
  mixins: [baseCommon(), filterCommon()],
  data() {
    return {
      list: [],
      tip: false,
    };
  },
  created() {
    this.getList();
  },
  mounted() { },
  methods: {
    setDefaultHouse(item, index) {
      if (item.defaultHouse === '1') {
        this.$message.warning({
          message: '该小区已是默认小区',
          duration: 1500,
        });
        return;
      }
      setDefaultHouse(item.userId, item.houseId)
        .then(() => this.$store.dispatch('GetUserInfo'))
        .then(() => {
          this.refresh(index);
          Bus.$emit('refershIndex');
          this.$message.success({
            message: '切换成功',
            duration: 1500,
          });
        });
    },
    refresh(i) {
      this.list.forEach((ele, index) => {
        if (index == i) {
          ele.defaultHouse = 1;
        } else {
          ele.defaultHouse = 0;
        }
      });
    },
    getList() {
      getList().then((res) => {
        const data = res.data;
        const obj = data.obj;
        this.list = obj;
      });
    },

  },
};
</script>

<style lang="scss" scoped>
</style>

